<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/24 0024
 * Time: 17:57
 */

namespace app\wap\controller;


use think\Db;

class Integral extends Middle
{
    private $UserIntegral;

    public function __construct()
    {
        parent::__construct();
        $this->UserIntegral = Db::name('user')->where(['uid'=>$this->uid])->value('integral');
    }

    //todo 我的积分
    public function IntegralUser(){
        return $this->returnSuccess($this->UserIntegral);
    }


	public function CashDesc(){
        $res = Db::name('common')->where(['type'=>86])->value('content');
        return $this->returnSuccess($res);
    }
    //todo 积分日志
    public function IntegralLogList(){
        $page = $this->page($this->data);
        $map = ['uid'=>$this->uid];
        $list = Db::name('integral_log')
            ->where($map)
            ->limit($page['page'],$page['limit'])
          	->order('create_time desc')
            ->field('create_time,desc,integral,type')
            ->select();
        return $this->returnSuccess($list);
    }

    //todo 积分兑换服务
    public function IntegralExchangeVip()
    {
        if($this->vip  == 2 && $this->vip_expire == 0)return $this->returnError('您已经是永久vip了');
        $map = ['id' => $this->data['id'], 'type' => 2];
        $list = Db::name('gear')->where($map)->find();
        if (!$list) return $this->returnError('不存在该项服务');
        if ($this->UserIntegral < $list['price']) return $this->returnError('积分不足,无法兑换');
      	$uvip   =Db::name('user_service')->where(['uid'=>$this->uid])->find();
      	$time = $uvip['expire'];
        if($uvip['expire'] < time())$time = time();
        $umap = ['uid'=>$this->uid];
        $sset = ['uid'=>$this->uid,'vip'=>$list['vip'],'expire'=>$time+$list['expire']*86400];
        Db::startTrans();
        try{
            Db::name('user')->where($umap)->setDec('integral',$list['price']);
            Db::name('user_service')->update($sset);
            $this->IntegralLog($this->uid,'兑换服务',2,$list['price']);
            Db::commit();
            return $this->returnSuccess(null,'兑换成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($this->Error);
        }
    }

    //todo 积分兑换实物
    public function IntegralExchangeThing()
    {
      	if ($this->UserIntegral < 1800)return $this->returnError('积分不足1888.无法使用兑换服务');
        if ($this->UserIntegral < $this->data['integral']) return $this->returnError('积分不足,无法兑换');     	
        $umap = ['uid'=>$this->uid];
      	if($this->data['integral'] <= 0) return $this->returnError('请输入正确的兑换积分');
        $gear = Db::name('gear')->where(['id'=>$this->data['id']])->find();
        if(!$gear)return $this->returnError('档位错误');
        $oset = ['addr'=>$this->data['addr'],'status'=>0,'create_time'=>time(),'desc'=>$gear['desc'],'price'=>$this->data['integral'],'uid'=>$this->uid,'num'=>round($this->data['integral']/$gear['price'],4)];
        Db::startTrans();
        try{
            Db::name('user')->where($umap)->setDec('integral',$this->data['integral']);
            Db::name('cash')->insert($oset);
            $this->IntegralLog($this->uid,'兑换币种',2,$this->data['integral']);
            Db::commit();
            return $this->returnSuccess(null,'兑换成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($e->getMessage());
        }
    }

    //todo 积分排名
    public function IntegralRank(){      	    
        $field = 'phone as nickname,integral,headimg';       
        $list = Db::name('user')
            ->order('integral desc')
            ->limit(20)
            ->field($field)
            ->select();
        return $this->returnSuccess($list);
    }

	public function IntegralDesc()
    {
      	
        $list = Db::name('common')
            ->where(['id'=>24])
          	->field('content,desc')
            ->select();
        return $this->returnSuccess($list);
    }

}